package com.aiju.cash.ui.account;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.aiju.app.SimpleHttpCallback;
import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.bean.Account;
import com.aiju.bean.ShopClerk;
import com.aiju.cash.R;
import com.aiju.cash.core.api.AccountApi;
import com.aiju.cash.util.GsonHelper;
import com.aiju.utils.T;

import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-03-28 09:59
 * @version: V1.0
 * @Description: 账本分享
 */
public class AccountShareActivity extends BaseTitleActivity {

    private TextView nameTextView;

    private Account mAccount;
    private ListView mListView;

    private MyBaseAdapter adapter;

    private AccountApi accountApi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_share);
        mAccount = (Account) getIntent().getSerializableExtra("account");
        nameTextView = (TextView) findViewById(R.id.tv_account_name);
        mListView = (ListView) findViewById(R.id.lv_account_share);
        nameTextView.setText(mAccount.getName());

        accountApi = new AccountApi(mDialogHelper);
        requsetClerkList();
    }

    /**
     * 获取店员列表
     */
    private void requsetClerkList() {
        accountApi.accountUserList(mAccount.getId(), new SimpleHttpCallback() {
            @Override
            protected void onDataSuccess(String data) {
                List<ShopClerk> clerks = GsonHelper.convertEntities(data, ShopClerk.class);
                adapter = new MyBaseAdapter(AccountShareActivity.this, clerks);
                mListView.setAdapter(adapter);
            }
        });
    }

    /**
     * 获取店员列表
     */
    private void requsetClerkShare(final ShopClerk clerk) {
        final boolean share = !clerk.isShare();
        accountApi.setAccountShare(mAccount.getId(), clerk.getOperatorId(), share, new SimpleHttpCallback() {
            @Override
            protected void onDataSuccess(String data) {
                if (share) {
                    T.ToastShort("分享成功");
                    clerk.setShareStatus("1");
                    adapter.notifyDataSetChanged();
                } else {
                    T.ToastShort("取消分享成功");
                    clerk.setShareStatus("2");
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(int code, String errorMessage) {
                if (share) {
                    T.ToastShort("分享失败");
                } else {
                    T.ToastShort("取消分享失败");
                }
            }
        });
    }

    private final class MyBaseAdapter extends BaseAdapter {

        private Context context;
        private List<ShopClerk> clerks;

        public MyBaseAdapter(Context context, List<ShopClerk> clerks) {
            this.context = context;
            this.clerks = clerks;
        }

        @Override
        public int getCount() {
            return clerks.size();
        }

        @Override
        public Object getItem(int position) {
            return clerks.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View rootView = LayoutInflater.from(context).inflate(R.layout.item_clerk, parent, false);

            TextView name = (TextView) rootView.findViewById(R.id.tv_clerk_name);
            TextView id = (TextView) rootView.findViewById(R.id.tv_clerk_id);
            TextView share = (TextView) rootView.findViewById(R.id.tv_clerk_share);
            ImageView type = (ImageView) rootView.findViewById(R.id.iv_clerk_type);

            final ShopClerk clerk = clerks.get(position);
            rootView.findViewById(R.id.ly_account_share).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    requsetClerkShare(clerk);
                }
            });
            share.setVisibility(View.VISIBLE);
            name.setText("昵称：" + clerk.getUserName());
            id.setText("账号：" + clerk.getPhone());
            if (clerk.isShare()) {
                share.setTextColor(Color.parseColor("#666666"));
                share.setText("已共享");
                type.setImageResource(R.mipmap.ic_account_shared);
            } else {
                share.setTextColor(Color.parseColor("#00a0e8"));
                share.setText("邀请共享");
                type.setImageResource(R.mipmap.ic_account_unshare);
            }
            return rootView;
        }
    }
}
